package org.zch.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.zch.common.utils.IpUtils;
import org.zch.dao.ReplyDAO;
import org.zch.entity.dto.CommentDTO;
import org.zch.entity.po.MessagePO;
import org.zch.entity.po.ReplyPO;
import org.zch.entity.po.UserInfoPO;
import org.zch.entity.vo.ReplyVO;
import org.zch.entity.vo.UserInfoVO;
import org.zch.service.MessageService;
import org.zch.service.ReplyService;
import org.springframework.stereotype.Service;
import org.zch.service.UserService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 回复表(Reply)表服务实现类
 *
 * @author 刘新
 * @since 2024-01-12 09:13:47
 */
@Service("replyService")
public class ReplyServiceImpl extends ServiceImpl<ReplyDAO, ReplyPO> implements ReplyService {

    @Autowired
    private ReplyDAO replyDAO;
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService msgService;

    /**
     * 发表回复
     * worksId：作品id
     * commentId：评论id
     * quitReplyCode:被回复人code
     * replyCode:回复人code
     * content:回复内容
     * location：ip地址
     *
     * @param replyPO
     * @return
     */
    @Override
    public ReplyVO insert(ReplyPO replyPO) {
        UserInfoVO replyUser = userService.getByCode(replyPO.getReplyCode());
        UserInfoVO quitUser = userService.getByCode(replyPO.getQuitReplyCode());
        replyPO.setReplyId(replyUser.getId());
        replyPO.setQuitReplyId(quitUser.getId());
        // 解析ip
        String location = this.getIp(replyPO.getLocation());
        replyPO.setLocation(location);
        replyPO.setCreateTime(new Date());
        int i = replyDAO.insert(replyPO);
        // 非自己回复自己时，发送消息
        if (!replyPO.getQuitReplyCode().equals(replyPO.getReplyCode())){
            MessagePO msgPO = new MessagePO();
            msgPO.setMsgContent(replyUser.getName() + "回复:" + replyPO.getContent() );
            msgPO.setSendUserId(replyUser.getId());
            msgPO.setSendUserCode(replyUser.getCode());
            msgPO.setToUserId(quitUser.getId());
            msgPO.setWorkId(replyPO.getWorksId());
            msgPO.setType("JUMP_INDEX");
            msgPO.setValue(replyPO.getWorksId().toString());
            msgPO.setToUserCode(quitUser.getCode());
            msgPO.setRelateId(replyPO.getId());
            msgPO.setSource(0);
            msgService.sendMsg(msgPO);
        }
        // 返回发表的单条数据
        return this.getReplyById(replyPO.getId());
    }

    /**
     * 根据作品id、评论id，查询回复
     *
     * @param dto 查询条件
     * @return 分页列表
     */
    @Override
    public IPage<ReplyVO> getReply(CommentDTO dto) {
        IPage<ReplyVO> reply = replyDAO.getReply(dto);
        List<ReplyVO> list = reply.getRecords();
        if (ArrayUtil.isAllNotEmpty(list)){
            // 批量查询用户信息
            List<String> userCodes = new ArrayList<>();
            for (ReplyVO replyVO : list) {
                userCodes.add(replyVO.getReplyCode());
                userCodes.add(replyVO.getQuitReplyCode());
            }
            List<UserInfoVO> userList = userService.getByCodes(userCodes);
            Map<String, List<UserInfoVO>> userMap = userList.stream().collect(Collectors.groupingBy(UserInfoPO::getCode));
            // 塞入用户昵称、头像
            for (ReplyVO replyVO : list) {
                String replyCode = replyVO.getReplyCode();
                String quitReplyCode = replyVO.getQuitReplyCode();
                List<UserInfoVO> replyUser = userMap.get(replyCode);
                List<UserInfoVO> quitReplyUser = userMap.get(quitReplyCode);
                replyVO.setReplyName(replyUser.get(0).getName());
                replyVO.setQuitReplyName(quitReplyUser.get(0).getName());
                replyVO.setReplyHeadPic(replyUser.get(0).getHeadPic());
                replyVO.setQuitReplyHeadPic(replyUser.get(0).getHeadPic());
            }
            // 对列表重新赋值
            reply.setRecords(list);
        }
        return reply;
    }


    /**
     * 根据id，查询单条数据
     * @param id 主键id
     * @return
     */
    public ReplyVO getReplyById(Integer id){
        CommentDTO dto = new CommentDTO();
        dto.setId(id);
        IPage<ReplyVO> reply = this.getReply(dto);
        if (ArrayUtil.isAllNotEmpty(reply)){
            return reply.getRecords().get(0);
        }else {
            return new ReplyVO();
        }
    }


    /**
     * IP 自定义内容
     *
     * @param ipLocation
     * @return
     */
    public String getIp(String ipLocation) {
        if (null == ipLocation) {
            return "未知星球";
        }
        String location = "";
        String province = IpUtils.getProvince(ipLocation);
        if (null != province) {
            location = location + province;
        } else {
            return "未知星球";
        }
        return location;
    }
}

